package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import algorithm.DescribeData;

/**
 * Servlet implementation class ServletForManager
 */
@WebServlet("/ServletForManager")
public class ServletForManager extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int numOfWork = -1;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServletForManager() {
		super();
		numOfWork = -1;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		
		String num = request.getParameter("num");
		if (num != null) {
			numOfWork = Integer.parseInt(num);
			numOfWork++;
		}
		
//		System.out.println("numofwork:" + numOfWork);
		
		Map<String, Object> map = new HashMap<>();
		String[] total = { "作业1", "作业2", "作业3", "作业4", "作业5", "作业6", "作业7", "作业8", "作业9", "作业10", "作业11", "作业12",
				"作业13", "作业14", "作业15", "作业16", "作业17", "作业18" };
		String[] titles = null;
		String[] groups = { "< 0", "(0, 2]", "(2, 4]", "(4, 6]", "(6, 8]", "(8, 10]" };
		map.put("groups", groups);
		
		Connection connection = null;
		try {

			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java", "root", "root");
			
			int maxn = 0;
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement.executeQuery("select max(id) from grade");
			while (resultSet.next()) {
				maxn = resultSet.getInt(1);
			}
			titles = new String[maxn];
			for (int i = 0; i < maxn; i++) {
				titles[i] = total[i];
			}
			map.put("titles", titles);
			
			if (numOfWork != -1) {
				int[] cnt = new int[6];
				String sql = "select * from grade where id = ?";
				PreparedStatement preparedStatement = connection.prepareStatement(sql);
				preparedStatement.setInt(1, numOfWork);
				resultSet = preparedStatement.executeQuery();
				while (resultSet.next()) {
					double score = resultSet.getDouble("score");
					if (score > 8 && score <= 10) {
						cnt[5]++;
					} else if (score > 6 && score <= 8) {
						cnt[4]++;
					} else if (score > 4 && score <= 6) {
						cnt[3]++;
					} else if (score > 2 && score <= 4) {
						cnt[2]++;
					} else if (score > 0 && score <= 2) {
						cnt[1]++;
					} else {
						cnt[0]++;
					}
				}

				map.put("cnt", cnt);
				
				
				DescribeData describeData = new DescribeData(numOfWork);
				double aver = describeData.computeAver();
				double stdev = describeData.computeStandardDeviation();
				map.put("aver", String.format("%.2f", aver).toString());
				map.put("stdev", String.format("%.2f", stdev).toString());
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (connection != null) {
					connection.close();
				}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		response.getWriter().println(JSON.toJSONString(map));

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
